<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">患者名字</label>
        <el-input v-model="query.patientName" clearable placeholder="患者名字" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">预约状态</label>
        <el-select
          v-model="query.status"
          clearable
          size="small"
          placeholder="预约状态"
          class="filter-item"
          style="width: 90px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in enabledTypeOptions"
            :key="item.key"
            :label="item.display_name"
            :value="item.key"
          />
        </el-select>

        <!--        <label class="el-form-item-label">预约状态</label>-->
        <!--        <el-input v-model="query.status" clearable placeholder="预约状态" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />-->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="患者名字" prop="patientId">
            <el-input v-model="form.patientName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="医生名字" prop="doctorId">
            <el-input v-model="form.doctorName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="预约时间" prop="appointmentTime">
            <el-date-picker v-model="form.appointmentTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="预约状态" prop="status">
            <el-radio v-model="form.status" label="0" disabled>预约未支付</el-radio>
            <el-radio v-model="form.status" label="1" disabled>预约成功</el-radio>
            <el-radio v-model="form.status" label="2" disabled>取消预约</el-radio>
            <el-radio v-model="form.status" label="3">到诊</el-radio>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="appointmentId" label="预约ID" />
        <el-table-column prop="patientName" label="患者名字" />
        <el-table-column prop="departmentName" label="科室名称" />
        <el-table-column prop="doctorName" label="医生名字" />
        <el-table-column prop="appointmentTime" label="预约时间" />
        <el-table-column prop="appointmentSlotsId" label="预约号" />
        <el-table-column prop="status" label="预约状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 0" type="warning">预约未支付</el-tag>
            <el-tag v-if="scope.row.status === 1" type="success">预约成功</el-tag>
            <el-tag v-if="scope.row.status === 2" type="danger">取消预约</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" />
        <!--        <el-table-column prop="updateTime" label="更新时间" />-->
        <el-table-column v-if="checkPer(['admin','appointment:edit','appointment:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <el-button type="info" icon="el-icon-message" size="mini" circle @click="callNumber(scope.row)">叫号</el-button>
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudAppointment, { callNumber } from '@/api/system/appointment'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation.vue'
import crudOperation from '@crud/CRUD.operation.vue'
import udOperation from '@crud/UD.operation.vue'
import pagination from '@crud/Pagination.vue'

const defaultForm = { appointmentId: null, patientId: null, doctorId: null, appointmentTime: null, status: null, createTime: null, updateTime: null, doctorName: null, patientName: null }
export default {
  name: 'Appointment',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '预约管理', url: 'api/appointment', idField: 'appointmentId', sort: 'appointmentId,desc', crudMethod: { ...crudAppointment }})
  },
  data() {
    return {
      enabledTypeOptions: [
        { key: '0', display_name: '预约未支付' },
        { key: '1', display_name: '预约成功' },
        { key: '2', display_name: '取消预约' }
      ],
      permission: {
        add: ['admin', 'appointment:add'],
        edit: ['admin', 'appointment:edit'],
        del: ['admin', 'appointment:del']
      },
      rules: {
        patientId: [
          { required: true, message: '患者ID不能为空', trigger: 'blur' }
        ],
        doctorId: [
          { required: true, message: '医生ID不能为空', trigger: 'blur' }
        ],
        appointmentTime: [
          { required: true, message: '预约时间不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '预约状态不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'appointmentTime', display_name: '预约时间' },
        { key: 'status', display_name: '预约状态' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 叫号
    callNumber(row) {
      console.log(row)
      callNumber({
        ids: row.patientId
      }).then(res => {
        console.log(res)
        this.$message.success('叫号成功')
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style scoped>

</style>
